﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.PrintExport;

namespace Aliyeye.Car.Net.UI.OrderModel
{
    public partial class MakeMoneyRenewal : Aliyeye.Car.Net.UI.BaseForm
    {
        ReserveSvc.SoapHeaderClass sopaHeaderReserveSvc = new Aliyeye.Car.Net.UI.ReserveSvc.SoapHeaderClass();
        ReserveSvc.Reserve modelReserve = new Aliyeye.Car.Net.UI.ReserveSvc.Reserve();
        CompanySvc.SoapHeaderClass sopaHeader = new CompanySvc.SoapHeaderClass();

        BillingDetailsSvc.SoapHeaderClass sopaHeaderBillSvc = new Aliyeye.Car.Net.UI.BillingDetailsSvc.SoapHeaderClass();

        private int reserveID = 0;
        private int customerID = 0;
        private DateTime endBooktime = DateTime.MinValue;

        BillingDetailsSvc.BillingDetails model = new Aliyeye.Car.Net.UI.BillingDetailsSvc.BillingDetails();
        Model.Customer customerInfo = new Model.Customer();
        bool isSave = false;

        public MakeMoneyRenewal()
        {
            InitializeComponent();

            this.lblIntoSectionDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.lblInputUserName.Text = Login.LogTrueName.ToString();
        }

        public MakeMoneyRenewal(int resID)
        {
            InitializeComponent();

            this.reserveID = resID;

            this.lblIntoSectionDate.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            this.lblInputUserName.Text = Login.LogTrueName.ToString();
            cmbIntoSectionType.SelectedIndex = 0;
        }

        private void MakeMoneyRenewal_Load(object sender, EventArgs e)
        {
            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;

            if (reserveID > 0)
            {
                ServiceManipulator.SafeAsyncExecution<ReserveSvc.ReserveServiceSoapClient>(
                    proxy => proxy.GetModel(sopaHeaderReserveSvc, reserveID),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            modelReserve = (ReserveSvc.Reserve)result.Result;
                            //客户信息
                            customerInfo.Code = modelReserve.CustomerCode;
                            customerInfo.Name = modelReserve.CustomerName;
                            customerInfo.ComName = modelReserve.ComName;

                            lblIntoSectionCode.Text = modelReserve.Code;

                            lblCustomerCode.Text = modelReserve.CustomerCode;
                            customerID = modelReserve.CustId;
                            endBooktime = modelReserve.EndBooktime;
                            lblOldEndDate.Text = endBooktime.ToString("yyyy-MM-dd HH:mm:ss");
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message);
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );

            }

            DataSet ds = null;
            ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeader, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            ds = (DataSet)result.Result;
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                Model.Company model = new Model.Company();

                                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                                model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                                model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                                model.EMail = ds.Tables[0].Rows[0]["EMail"].ToString();
                                model.BankName = ds.Tables[0].Rows[0]["BankName"].ToString();
                                model.BankAccount = ds.Tables[0].Rows[0]["BankAccount"].ToString();
                                lblReceivingBankCom.Text = model.ComName.ToString();

                                lblReceivingBank.Text = model.BankName;
                                lblReceivingBankAccount.Text = model.BankAccount;
                            }
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message);
                            overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
        }

        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //入款方式
        private void cmbIntoSectionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbIntoSectionType.Text.Trim() == "现金")
            {
                lblImportBank.Visible = false;
                txtImportBank.Visible = false;
                lblImportBankAccount.Visible = false;
                txtImportBankAccount.Visible = false;

                lblCollectionType.Visible = true;
                txtCollectionType.Visible = true;
                lblCollectionTypeMsg.Visible = true;
            }
            else
            {
                lblImportBank.Visible = true;
                txtImportBank.Visible = true;
                lblImportBankAccount.Visible = true;
                txtImportBankAccount.Visible = true;

                lblCollectionType.Visible = false;
                txtCollectionType.Visible = false;
                lblCollectionTypeMsg.Visible = false;
            }
        }

        private void txtImportAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            IsNumberMoney(txtImportAmount, e);
        }

        private bool IsNumberMoney(System.Windows.Forms.TextBox t, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '.' || e.KeyChar == 8 || e.KeyChar == 13))
            {
                return true;
            }
            else
            {
                PageCommon.Alert("入账金额只能输入数字！");
                e.Handled = true;
                return false;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtImportAmount.Text))
            {
                PageCommon.Alert("入账金额不能为空，请输入！");
                return;
            }
            else if (Convert.ToDecimal(txtImportAmount.Text) <= 0)
            {
                PageCommon.Alert("入账金额不能小于等于零，请重新输入！");
                return;
            }

            if (cmbIntoSectionType.Text.Trim() == "现金")
            {
                if (string.IsNullOrEmpty(txtCollectionType.Text))
                {
                    PageCommon.Alert("收款方式不能为空，请输入！");
                    return;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(txtImportBank.Text))
                {
                    PageCommon.Alert("汇入银行不能为空，请输入！");
                    return;
                }
                if (string.IsNullOrEmpty(txtImportBankAccount.Text))
                {
                    PageCommon.Alert("账号不能为空，请输入！");
                    return;
                }
            }

            DateTime dtEDate = dtpEndBookDate.Value;
            DateTime dtETime = dtpEndBookingtime.Value;
            DateTime dtTemp = new DateTime(dtEDate.Year, dtEDate.Month, dtEDate.Day, dtETime.Hour, dtETime.Minute, dtETime.Second);

            if (dtTemp.Subtract(endBooktime).TotalSeconds < 0)
            {
                PageCommon.Alert("延顺还车日期不能小于原还车日期！");
                return;
            }


            //sopaHeaderBillSvc

            model.CollectionType = txtCollectionType.Text;
            model.CustomerID = this.customerID;
            model.EndBookDate = dtTemp;
            model.ImportAmount = Convert.ToDecimal(txtImportAmount.Text);
            model.ImportBank = txtImportBank.Text;
            model.ImportBankAccount = txtImportBankAccount.Text;
            model.InpuTime = DateTime.Now;
            model.InputUserID = Login.LogUserId;
            model.IntoSectionContent = "补款续租";
            model.IntoSectionDate = DateTime.Now;
            model.IntoSectionID = this.reserveID;
            model.IntoSectionType = cmbIntoSectionType.Text;
            model.ReceivingBank = lblReceivingBank.Text;
            model.ReceivingBankAccount = lblReceivingBankAccount.Text;
            model.ReceivingBankCom = lblReceivingBankCom.Text;

            //添加
            ServiceManipulator.SafeAsyncExecution<BillingDetailsSvc.BillingDetailsServiceSoapClient>(
                proxy => proxy.Add(sopaHeaderBillSvc, model),
                result =>
                {
                    if (result.Error == null)
                    {
                        PageCommon.Notify("补款成功！");
                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        isSave = true;
                        txtCollectionType.Enabled = false;
                        dtpEndBookDate.Enabled = false;
                        dtpEndBookingtime.Enabled = false;
                        txtImportAmount.Enabled = false;
                        txtImportBank.Enabled = false;
                        txtImportBankAccount.Enabled = false;
                        cmbIntoSectionType.Enabled = false;
                        btnSave.Enabled = false;
                        //reserveID = modelReserve.Id = Convert.ToInt32(result.Result);
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message);
                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
            //PageCommon.Notify("还没实现哦，请耐心等待。");
        }
        //打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (!isSave)
            {
                PageCommon.Notify("请先入账，再打印单据！");
                return;
            }

            DataSet ds = null;
            CompanySvc.SoapHeaderClass sopaHeaderCompany = new CompanySvc.SoapHeaderClass();
            overlay.FadeIn(); Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeaderCompany, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            ds = (DataSet)result.Result;
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                Model.Company modelCom = new Model.Company();

                                modelCom.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                                modelCom.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                                modelCom.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                                modelCom.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                                modelCom.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                                modelCom.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();

                                //开始打印结算单
                                WordHelper wordHelp = new WordHelper();
                                if (wordHelp.PrintMakeMoneyRenewal(modelCom, customerInfo, modelReserve, model, Login.LogTrueName, lblOldEndDate.Text))
                                {
                                    PageCommon.Notify("打印成功！");
                                    overlay.FadeOut(); Cursor.Current = Cursors.Default;
                                }
                                else
                                {
                                    PageCommon.Alert("打印失败！");
                                    overlay.FadeOut(); Cursor.Current = Cursors.Default;
                                }
                            }
                            else
                            {
                                PageCommon.Alert("打印失败！");
                                overlay.FadeOut(); Cursor.Current = Cursors.Default;
                            }
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
        }


    }
}
